package com.example.dllo.keepdemo.person;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.person.bean.PersonDynamicBase;
import com.example.dllo.keepdemo.tools.CalculateTimeDifference;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;

/**
 * Created by dllo on 16/1/21.
 */
public class PersonDynamicListViewAdapter extends BaseAdapter {
    private Context context;
    private PersonDynamicBase data;
    private String userName;
    private String headUrl;

    public PersonDynamicListViewAdapter(Context context,PersonDynamicBase data,String userName,String headUrl) {
        this.context = context;
        this.data = data;
        this.userName = userName;
        this.headUrl = headUrl;
    }

    @Override
    public int getCount() {
        return data.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_activity_personmessage,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        myViewHolder.tvName.setText(userName);
        myViewHolder.tvTime.setText(CalculateTimeDifference.timeDifferenceUTC(data.getData().get(position).getCreated()));
        myViewHolder.tvContent.setText(data.getData().get(position).getContent());
        myViewHolder.tvPraise.setText(data.getData().get(position).getLikes() + "");
        myViewHolder.tvComments.setText(data.getData().get(position).getComments() + "");

        if (headUrl.equals("")) {
            myViewHolder.imgHead.setImageResource(R.mipmap.person_45_45);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgHead,R.mipmap.person_45_45,R.mipmap.person_45_45);
            VolleySingleton.getImageLoader().get(headUrl,listener);
        }

        if (data.getData().get(position).getPhoto().equals("")) {
            myViewHolder.imageView.setVisibility(View.GONE);
        } else {
            myViewHolder.imageView.setVisibility(View.VISIBLE);
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView,R.mipmap.no_photo,R.mipmap.no_photo);
            VolleySingleton.getImageLoader().get(data.getData().get(position).getPhoto(),listener,550,550);
        }

        return convertView;
    }

    class MyViewHolder {
        private RoundImageView imgHead;
        private ImageView imageView;
        private TextView tvName,tvTime,tvContent,tvPraise,tvComments;
        public MyViewHolder(View view) {
            imgHead = (RoundImageView) view.findViewById(R.id.img_item_listview_activity_personmessage_headpicture);
            imageView = (ImageView) view.findViewById(R.id.img_item_listview_activity_personmessage_picture);
            tvName = (TextView) view.findViewById(R.id.tv_item_listview_activity_personmessage_name);
            tvTime = (TextView) view.findViewById(R.id.tv_item_listview_activity_personmessage_time);
            tvContent = (TextView) view.findViewById(R.id.tv_item_listview_activity_personmessage_content);
            tvPraise = (TextView) view.findViewById(R.id.tv_item_listview_activity_personmessage_praisenum);
            tvComments = (TextView) view.findViewById(R.id.tv_item_listview_activity_personmessage_replynum);
        }
    }
}
